//
//  YDCompareTableView.m
//  ModelContrast
//
//  Created by Cluy on 15/6/29.
//  Copyright (c) 2015年 sunflowers. All rights reserved.
//

#import "YDCompareTableView.h"
#import "YDTableCell.h"

@implementation YDCompareTableView

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString *CellIdentifier = [NSString stringWithFormat:@"YDTableCell%zd",indexPath.section];
    
    [self registerClass:[YDTableCell class] forCellReuseIdentifier:CellIdentifier];
    
    
    YDTableCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    
    NSUInteger row = [indexPath row];
    if (_isSelectBtn2 ==YES) {
        cell.label2.text =[[self.sectionList  objectAtIndex:indexPath.section]objectAtIndex:row];
        if (_isSelectBtn3 == YES) {
            cell.label3.text = [[self.sectionList objectAtIndex:indexPath.section]objectAtIndex:row];
        }
    }
    if ([self.sectionList count]<=0) {
        return cell;
    }
    NSArray *cellData =[self.sectionList objectAtIndex:indexPath.section];
    cell.labe1.text = [cellData objectAtIndex:row];
    self.cellHeight = [self getHeightOfLabel1Text:cell.labe1.text WithLabel2Text:cell.label2.text WithLabel3Text:cell.label3.text];
    if (self.cellHeight <40) {
        self.cellHeight = 40;
    }
    cell.labe1.frame = CGRectMake(cell.labe1.frame.origin.x,
                                                          cell.labe1.frame.origin.y,
                                                          cell.labe1.frame.size.width,
                                                          self.cellHeight);
    cell.label2.frame = CGRectMake(cell.label2.frame.origin.x,
                                  cell.label2.frame.origin.y,
                                  cell.label2.frame.size.width,
                                  self.cellHeight);

    cell.label3.frame = CGRectMake(cell.label3.frame.origin.x,
                                  cell.label3.frame.origin.y,
                                  cell.label3.frame.size.width,
                                  self.cellHeight);

    cell.frame =CGRectMake(cell.frame.origin.x,
                           cell.frame.origin.y,
                           cell.frame.size.width,
                           self.cellHeight);
    
    
    //设置cell 高亮 和 差异化
    if (_isDifferent) {
        if ( [self isDiffForCellText:cell]) {
            self.cellHeight =0;
        }
    } else {
        
        if ( ![self isDiffForCellText:cell]){
            cell.labe1.textColor = [UIColor redColor];
            cell.label2.textColor = [UIColor redColor];
            cell.label3.textColor = [UIColor redColor];
        }
    }
    return cell;
}
//高亮 yes 为相同同 no 为不相同
- (BOOL)isDiffForCellText:(YDTableCell *)cell{
    if (cell.labe1.text && cell.label2.text) {
        if ([cell.labe1.text isEqualToString:cell.label2.text]) {
            if(!cell.label3.text || [cell.label2.text isEqualToString:cell.label3.text]){
                return YES;
            }
        }
    }else
    {   //只有Lable1的情况下 字体为默认颜色
        return YES;
    }
    return NO;
}
- (float) getHeightOfLabel1Text:(NSString*)text WithLabel2Text:(NSString*)label2Text WithLabel3Text:(NSString*)label3Text{
    NSString *tempStr = text;
    if ([text length] >[label2Text length]) {
        if ([text length] >[label3Text length]) {
            tempStr = text;
        }else{
            tempStr = label3Text;
        }
    }else{
        if ([label2Text length] >[label3Text length]) {
            tempStr =label2Text;
        }else{
            tempStr = label3Text;
        }
    }
  return [self getHeightWithText:tempStr];
}
- (float) getHeightWithText:(NSString*)text
{
    if (text.length == 0)
    {
        return 40;
    }
    CGSize size = CGSizeMake(324, CGFLOAT_MAX);
    CGSize labelsize = [text sizeWithFont:[UIFont systemFontOfSize:17]
                        constrainedToSize:size
                        lineBreakMode:NSLineBreakByWordWrapping];
    
    return labelsize.height;
}



- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if(_changeCellDelegate){
        return   [_changeCellDelegate tableView:tableView indexPath:indexPath];
    }
 
    return self.cellHeight;
}
@end
